<template>
    <kdx-modal-frame v-model="value" title="确认正在归还中" :width="900" @on-cancel="handleCancel" @on-ok="handleOk">
        <Form ref="form_info" :model="model" :rules="rules" :label-width="120" class="edit-information-modal">
            <FormItem label="归还时间" :label-width="95" prop="lease_return_at">
                <DatePicker
                    v-model="model.lease_return_at"
                    type="date"
                    placeholder="归还时间"
                    format="yyyy-MM-dd"
                    style="width: 340px;"
                ></DatePicker>
            </FormItem>
            <div class="content" style="padding: 12px 16px;">
                <table class="ivu-table" cellspacing="0" style="width: 100%;">
                    <tr>
                        <th class="text-center" style="width: 170px;">快递公司</th>
                        <th class="text-center">快递单号</th>
                        <th class="text-center">备注</th>
                    </tr>
                    <tr v-for="(item, index) in express_data" :key="index">
                        <td class="text-center">
                            <Row>
                                <i-col span="7">
                                    <Button
                                        size="small"
                                        type="warning"
                                        @click="handleDelete(index)"
                                        style="margin-top: 3px;"
                                    >删除</Button>
                                </i-col>
                                <i-col span="1"></i-col>
                                <i-col span="16">
                                    <Select
                                        v-model="item.express_id"
                                        class="width-110"
                                        prop="express_id"
                                    >
                                        <Option
                                            v-for="exp in express"
                                            :key="exp.id"
                                            :value="exp.id"
                                        >
                                            {{ exp.name }}
                                        </Option>
                                    </Select>
                                </i-col>
                            </Row>
                        </td>
                        <td class="text-center" style="padding: 10px 0;" >
                            <Input
                                v-model="item.express_sn"
                                style="width: 340px;"
                                placeholder="请输入单号"
                            />
                        </td>
                        <td class="text-center" style="padding: 10px 0;" >
                            <Input
                                v-model="item.remark"
                                style="width: 340px;"
                                placeholder="请输入备注"
                            />
                        </td>
                    </tr>
                </table>
                <Button type="success" @click="handleAdd()" style="margin-top: 10px;">
                    + 增加物流信息
                </Button>
            </div>
        </Form>
    </kdx-modal-frame>
</template>

<script>

    export default {
        name: "ConfirmReturning",
        props: {
            lease_return_at: {
                type: [String]
            },
            id: {
                type: [Number, String]
            },
        },
        data() {
            return {
                value: false,
                model: {
                    lease_return_at: '',
                    express_id: '',
                    express_sn: '',
                },
                rules: {
                    lease_return_at: [{
                        required: true,
                        message: '归还时间必填'
                    }],
                    express_id: [
                        {required: true, message: '快递公司必选'}
                    ],
                    express_sn: [
                        {required: true, message: '快递单号必填'}
                    ],
                },
                express_data: [
                    {
                        "express_id":"1",
                        "express_sn":"",
                        "remark":"",
                    }
                ],
                express: [
                    {
                        "id":"1",
                        "name":"顺丰",
                        "code":"shunfeng",
                        "key":"SF"
                    },
                    {
                        "id":"2",
                        "name":"申通",
                        "code":"shentong",
                        "key":"STO"
                    },
                    {
                        "id":"3",
                        "name":"韵达快运",
                        "code":"yunda",
                        "key":"YD"
                    },
                    {
                        "id":"4",
                        "name":"天天快递",
                        "code":"tiantian",
                        "key":"HHTT"
                    },
                    {
                        "id":"5",
                        "name":"圆通速递",
                        "code":"yuantong",
                        "key":"YTO"
                    },
                    {
                        "id":"6",
                        "name":"中通速递",
                        "code":"zhongtong",
                        "key":"ZTO"
                    },
                    {
                        "id":"7",
                        "name":"ems快递",
                        "code":"ems",
                        "key":"EMS"
                    },
                    {
                        "id":"8",
                        "name":"百世快递",
                        "code":"huitongkuaidi",
                        "key":"HTKY"
                    },
                    {
                        "id":"92",
                        "name":"京东快递",
                        "code":"jd",
                        "key":"JD"
                    },
                    {
                        "id":"93",
                        "name":"众邮快递",
                        "code":"zhongyouex",
                        "key":"ZYE"
                    },
                    {
                        "id":"94",
                        "name":"极兔速递",
                        "code":"jtexpress",
                        "key":"JTSD"
                    },
                    {
                        "id":"95",
                        "name":"中通快运",
                        "code":"zhongtongkuaiyun1",
                        "key":"ZTOKY"
                    }
                ]
            };
        },
        methods: {
            setValue() {
                this.value = !this.value;
                if (this.value) {
                    let lease_return_at = this.lease_return_at
                    if (lease_return_at == 0) {
                        lease_return_at = new Date().valueOf() / 1000
                    }
                    this.model = {
                        lease_return_at: this.$moment.unix(lease_return_at).format("YYYY-MM-DD"),
                    };
                }
            },
            handleCancel() {
                this.setValue();
            },
            handleOk() {
                // 调用接口
                this.$refs['form_info'].validate(valid => {
                    if (valid) {
                        let lease_return_at = this.$moment.unix(this.model.lease_return_at).unix() / 1000 + 43200;
                        for(let i in this.express_data) {
                            if(this.express_data[i]['express_id'] == '') {
                                alert('请选择快递公司')
                                return
                            }
                            if(this.express_data[i]['express_sn'] == '') {
                                alert('请填写快递单号')
                                return
                            }
                        }
                        let params = {
                            order_id: this.id, 
                            return_at: lease_return_at,
                            express_data: this.express_data
                        };
                        this.$api.orderApi.setConfirmReturning(params)
                            .then(res => {
                                if (res.error === 0) {
                                    !this.isExternal && this.setValue();
                                    this.$emit('on-refresh');
                                    location. reload()
                                }
                            });
                    }
                });
            },
            handleDelete(index) {
                this.express_data.splice(index)
            },
            handleAdd() {
                this.express_data.push({
                        "express_id":"1",
                        "express_sn":"",
                        "remark":"",
                    })
            },
        }
    };
</script>

<style scoped lang="scss">
    .edit-information-modal {
        padding-top: 40px;
    }
</style>
